-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
updated custom error func #94
updated custom error func #94
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes here should enable you to put whatever you like in errors. "extra" isn't part of the spec, its something you've come up with, so it shouldn't be directly referenced in gqlgen. Perhaps in a test, so you know your use case is covered, but not in a public interface.
// MarshalJSON marshals an interface into JSON. | ||
func MarshalJSON(i interface{}) Marshaler { | ||
return WriterFunc(func(w io.Writer) { | ||
json.NewEncoder(w).Encode(i) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an error being ignored here, I wonder why gometalinter isn't complaining.
} | ||
|
||
// ErrorMessageFunc a func which given an error returns a formatted error. | ||
type ErrorMessageFunc func(err error) FormattedError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is quite right. It only lets the user change whats in this "extra" key.
Maybe it could return a graphql.Marshaler?
@@ -29,10 +30,11 @@ func Errorf(format string, a ...interface{}) *QueryError { | |||
|
|||
// WithMessagef is the same as Errorf, except it will store the err inside | |||
// the ResolverError field. | |||
func WithMessagef(err error, format string, a ...interface{}) *QueryError { | |||
func WithMessagef(err error, extra interface{}, format string, a ...interface{}) *QueryError { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the worst kind of BC break.
WithMessagef(err, "hello %s", name)
will continue to compile but the format string will dumped into extra and the name into the format string.
@@ -18,6 +18,8 @@ import ( | |||
"github.com/vektah/gqlgen/test/models" | |||
) | |||
|
|||
type fErr = gqlerrors.FormattedError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a fan of abusing type aliases like this.
updated custom error func to allow for extra data to be returned and rendered as JSON. Fixes #90